﻿#include "cmodbusunitbase.h"
#include <shlib/csigmanager.h>

CModbusUnitBase::CModbusUnitBase(int colnum, ESigType maptype, QObject *parent)
    : IModel((colnum+5), parent),map_type(maptype) {
    m_fieldEditableMap.insert("映射ID", false);
    m_fieldEditableMap.insert("信号名字", false);

    QMap<int, QString> vmap;
    vmap.insert(0, "否");
    vmap.insert(1, "是");
    m_fieldValueMap.insert("是否过滤", vmap);
}

void CModbusUnitBase::commit() {
    is_init = true;
    m_isDelete = false;
    m_ischange = false;
    opt_type = EOptType::EOptUnDefine;
    changeBgColor(false);
}

QString CModbusUnitBase::getFieldNameByCol(int col) {
    if(col < 0 || col >= EInvalid)
        return "";

    switch (col) {
    case EModbusUnitCol::EMapId:
        return "映射ID";
    case EModbusUnitCol::EName:
        return "信号名字";
    case EModbusUnitCol::ERegType:
        return "寄存器类型";
    case EModbusUnitCol::EAddress:
        return "寄存器地址";
    case EModbusUnitCol::EFilter:
        return "是否过滤";
    default:
        break;
    }
    return "";
}

QVariant CModbusUnitBase::getFieldValueByFielddName(const QString &name)
{
    if("映射ID" == name) {
        return map_id.split(".").last();
    } else if("信号名字" == name) {
        return this->name;
    } else if("寄存器类型" == name) {
        return reg_type;
    } else if("寄存器地址" == name) {
        return address;
    } else if("是否过滤" == name) {
        return isfilter ? 1 : 0;
    }
    return QVariant();
}

void CModbusUnitBase::setFieldValueByCol(int col, const QVariant &value)
{
    if(ERegType == col) {
        reg_type = value.toInt();
        changeBgColor(true);
    } else if(EAddress == col) {
        address = value.toInt();
        changeBgColor(true);
    } else if(EFilter == col) {
        isfilter = value.toInt();
        changeBgColor(true);
    }
}

QJsonObject CModbusUnitBase::toJson() {
    QJsonObject obj;
    obj["map_id"] = map_id;
    obj["name"] = name;
    obj["reg_type"] = reg_type;
    obj["address"] = address;
    obj["is_filter"] = isfilter;
    obj["init"] = is_init;

    QString optstr;
    if(opt_type == EOptType::EAdd) {
        optstr = "opt_add";
    } else if(opt_type == EOptType::EChange) {
        optstr = "opt_change";
    } else if(opt_type == EOptType::EDelete) {
        optstr = "opt_delete";
    } else if(opt_type == EOptType::ESelect) {
        optstr = "opt_select";
    } else {
        optstr = "opt_undefine";
    }
    obj["opt"] = optstr;
    return obj;
}

void CModbusUnitBase::fromJson(const QJsonObject &obj) {
    map_id = obj["map_id"].toString();
    name = obj["name"].toString();
    reg_type = obj["reg_type"].toInt();
    address = obj["address"].toInt();
    isfilter = obj["is_filter"].toBool();
    is_init = obj["init"].toBool();

    opt_type = EOptType::EOptUnDefine;
    changeBgColor(false);
}

QSharedPointer<IModel> CModbusUnitBase::createEmpty()
{

}

void CModbusUnitBase::changeBgColor(bool ischange) {
    if(!ischange) {
        m_backgroundField.clear();
        m_ischange = false;
    } else {
        m_backgroundField.insert("寄存器类型", "#FFA49F");
        m_backgroundField.insert("寄存器地址", "#FFA49F");
        m_backgroundField.insert("是否过滤", "#FFA49F");
        m_ischange = true;

        if(is_init) {
            if(m_isDelete) {
                opt_type = EOptType::EDelete;
            } else {
                opt_type = EOptType::EChange;
            }
        } else {
            opt_type = EOptType::EAdd;
        }

        CSigMgr->modbusMapSigChange(name, map_type);
    }
}

bool CModbusUnitBase::checkData() {
    return !(address < 0);
}

void CModbusUnitBase::deleteReset() {
    address = -1;
    isfilter = false;
    is_init = false;
    m_ischange = false;
    m_isDelete = false;
    m_backgroundField.insert("寄存器类型", "#6B6B6B");
    m_backgroundField.insert("寄存器地址", "#6B6B6B");
    m_backgroundField.insert("是否过滤", "#6B6B6B");
}
